package org.zeapp.opencloud.ui.preferences;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import org.zeapp.opencloud.framework.synchro.FolderAlias;

/**
 * The table model to display FolderAlias in the preference JTable
 * 
 * @author Amphiprion
 * 
 */
@SuppressWarnings("serial")
public class FolderAliasTableModel extends DefaultTableModel {
	private List<FolderAlias> aliases;

	public void setFolderAliases(List<FolderAlias> aliases) {
		this.aliases = aliases;
		fireTableDataChanged();
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return String.class;
	}

	@Override
	public int getColumnCount() {
		return 2;
	}

	@Override
	public String getColumnName(int columnIndex) {
		switch (columnIndex) {
		case 0:
			return "Alias";
		case 1:
			return "Local path";
		default:
			return "???";
		}
	}

	@Override
	public int getRowCount() {
		if (aliases == null) {
			return 0;
		}
		return aliases.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return aliases.get(rowIndex).getName();
		case 1:
			return aliases.get(rowIndex).getLocalPath();
		default:
			return "???";
		}
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return true;
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			aliases.get(rowIndex).setName((String) aValue);
			return;
		case 1:
			aliases.get(rowIndex).setLocalPath((String) aValue);
			return;
		}
	}

	/**
	 * Return the folder alias.
	 * 
	 * @param rowIndex
	 *            the row index
	 * @return the folder alias or null
	 */
	public FolderAlias getFolderAlias(int rowIndex) {
		if (aliases != null && aliases.size() > rowIndex) {
			return aliases.get(rowIndex);
		}
		return null;
	}

	public List<FolderAlias> getFolderAliases() {
		return aliases;
	}

	public void addRow(FolderAlias folderAlias) {
		if (aliases == null) {
			aliases = new ArrayList<FolderAlias>();
		}
		aliases.add(folderAlias);
		fireTableDataChanged();

	}
}
